#pragma once
#include <vector>
#include <thread>
#include <future>
#include <iostream>
#include <algorithm>
#include <functional>
#include <random>
template<class T>
void _MergeSort(std::vector<T>& nums, int l, int r, std::vector<T>& tmp, std::function<bool(T, T)> cmp = std::less<T>()) {
	if (l >= r) {
		return;
	}
	int mid = l + (r - l) / 2;
	_MergeSort(nums, l, mid, tmp, cmp);
	_MergeSort(nums, mid + 1, r, tmp, cmp);
	/*int i = l, j = mid + 1, k = l;
	while (i <= mid && j <= r) {
		if (cmp(nums[i], nums[j])) {
			tmp[k] = nums[i];
			++k;
			++i;
		}
		else {
			tmp[k] = nums[j];
			++k;
			++j;
		}
	}
	while (i <= mid) {
		tmp[k] = nums[i];
		++k;
		++i;
	}
	while (j <= r) {
		tmp[k] = nums[j];
		++k;
		++j;
	}
	std::copy(tmp.begin() + l, tmp.begin() + r + 1, nums.begin() + l);*/
	std::merge(nums.begin() + l, nums.begin() + mid + 1,
		nums.begin() + mid + 1, nums.begin() + r + 1,
		tmp.begin() + l, cmp);
	std::copy(tmp.begin() + l, tmp.begin() + r + 1, nums.begin() + l);
}
template<class T>
void MergeSort(std::vector<T>& nums, std::function<bool(T, T)> cmp = std::less<T>()) {
	std::vector<T> tmp(nums.size());
	_MergeSort(nums, 0, nums.size() - 1, tmp, cmp);
}